<template>
  <ma-crud ref="crudRef" :columns="columns" :options="crud">
    <template #search-companyName="{ searchForm, component }">
      <a-input
        v-model.trim="searchForm[component.dataIndex]"
        :style="{ width: '240px' }"
        allow-clear
        placeholder="请输入企业名称"
      >
        <template #prefix>
          <icon-search />
        </template>
      </a-input>
    </template>
    <template #operationBeforeExtend="{ record }">
      <a-link>同步</a-link>
    </template>
  </ma-crud>
</template>
<script lang="ts" name="Tenant" setup>
import * as Regexp from "@/utils/regexp";
import { addTenant, delTenant, listTenant, updateTenant } from "@/api/system/tenant";

const crudRef = ref();
const crud = reactive({
  id: "id",
  pk: "id",
  title: "租户查询",
  formLayout: "vertical",
  formOption: {
    width: 700,
    layout: [
      {
        formType: "grid",
        gutter: 16,
        colNumber: 2,
        cols: [
          { formList: [{ dataIndex: "companyName" }] },
          { formList: [{ dataIndex: "contactUserName" }] },
          { formList: [{ dataIndex: "contactPhone" }] },
          { formList: [{ dataIndex: "username" }] },
          { formList: [{ dataIndex: "password" }] },
          { formList: [{ dataIndex: "packageId" }] },
          { formList: [{ dataIndex: "expireTime" }] },
          { formList: [{ dataIndex: "accountCount" }] },
          { formList: [{ dataIndex: "status" }] },
          { formList: [{ dataIndex: "remark" }] }
        ]
      }
    ]
  },
  api: listTenant,
  showIndex: false,
  operationColumn: true,
  operationColumnAlign: "center",
  operationColumnWidth: 180,
  pageLayout: "normal",
  requestParams: { orderBy: "createTime", orderType: "desc" },
  add: { api: addTenant, auth: ["system:tenant:add"], text: "新增", show: true },
  edit: { api: updateTenant, auth: ["system:tenant:edit"], text: "编辑", show: true },
  delete: { api: delTenant, auth: ["system:tenant:delete"], text: "删除", show: true }
});

const columns = reactive([
  {
    title: "编号",
    dataIndex: "id",
    align: "center",
    hide: true,
    addDisplay: false,
    editDisplay: false,
    onControl: (value: any, maFormObject: any) => {
      const service = maFormObject.getColumnService();
      const dataIndexList = ["username", "password"];
      if (value) {
        dataIndexList.map(name => service.get(name).setAttr("display", false));
      }
    }
  },
  {
    title: "租户编号",
    dataIndex: "tenantId",
    align: "center",
    hide: true,
    addDisplay: false,
    editDisplay: false
  },
  {
    title: "企业名称",
    dataIndex: "companyName",
    align: "center",
    search: true,
    commonRules: [{ required: true, message: "企业名称不能为空" }]
  },
  {
    title: "联系人",
    dataIndex: "contactUserName",
    align: "center",
    commonRules: [{ required: true, message: "联系人不能为空" }]
  },
  {
    title: "联系电话",
    dataIndex: "contactPhone",
    align: "center",
    commonRules: [
      { required: true, message: "联系电话不能为空" },
      { match: Regexp.Phone, message: "请输入正确的手机号码" }
    ]
  },
  {
    dataIndex: "username",
    title: "用户名",
    hide: true,
    editDisplay: false,
    commonRules: [
      { required: true, message: "用户名不能为空" },
      {
        min: 2,
        max: 20,
        message: "用户名称长度必须介于 2 和 20 之间"
      }
    ]
  },
  {
    dataIndex: "password",
    title: "用户密码",
    formType: "input-password",
    hide: true,
    editDisplay: false,
    commonRules: [
      { required: true, message: "用户密码不能为空" },
      {
        min: 5,
        max: 20,
        message: "用户密码长度必须介于 5 和 20 之间"
      }
    ]
  },
  {
    dataIndex: "packageId",
    title: "请选择租户套餐",
    formType: "select",
    hide: true,
    editDisabled: true,
    dict: {
      url: "system/tenantPackage/selectList",
      method: "get",
      openPage: false,
      props: { label: "packageName", value: "packageId" },
      cache: false
    }
  },
  {
    title: "过期时间",
    dataIndex: "expireTime",
    align: "center",
    formType: "date",
    showTime: true,
    tooltip: "租户过期不选择则表示不限制"
  },
  {
    dataIndex: "accountCount",
    title: "用户数量",
    formType: "inputNumber",
    hide: true,
    addDefaultValue: -1,
    tooltip: "租户数量为-1表示不限制"
  },
  {
    title: "租户状态",
    dataIndex: "status",
    align: "center",
    formType: "radio",
    dict: {
      name: "sys_normal_disable",
      cache: true,
      props: { label: "dictLabel", value: "dictValue" },
      translation: true,
      tagColors: true
    },
    addDefaultValue: 0
  },
  {
    dataIndex: "remark",
    title: "描述",
    autoSize: { minRows: 3, maxRows: 6 },
    maxLength: 200,
    showWordLimit: true
  }
]);
</script>
